package com.swss.g.dept.action;

import java.util.List;

import com.swss.g.sys.service.SysService;
import com.swss.g.user.bean.User;
import com.swss.g.base.BaseAction;
import com.swss.g.dept.bean.Dept;
import com.swss.g.dept.service.DeptService;
import com.swss.util.BeanUtils;
import com.swss.util.Pagination;

public class DeptAction extends BaseAction {
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 2946633789255494944L;

	/**
	 * bean
	 */
	private String deptNo;

	private Dept dept;

	private Pagination<?> pagination;

	private int count;

	private int flag;

	private List<Dept> deptList;

	private String url;

	/**
	 * service
	 * 
	 * @return
	 */

	private DeptService deptService;
	private SysService sysService;

	@SuppressWarnings("unchecked")
	public String findDeptById() {
		try {

			Dept d = new Dept();
			d.setDeptName("222");
			Pagination p = new Pagination();
			p.setUsepage(false);

			// userList = this.getUserService().findByExample(u, p);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// return INPUT;
		}
		return SUCCESS;
	}

	public String add() {
		try {

			flag = this.getDeptService().insertDeptInfo(this.dept);
			if (flag == 1) {
				this.msg = "单位添加成功";
			} else if (flag == 2) {
				this.msg = "无上级单位";
			} else {
				this.msg = "单位编号重复";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	};

	public String del() {
		try {
			this.dept = this.getDeptService().get(this.dept.getId());
			this.getDeptService().deleteDeptInfo(dept);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}

	public String update() {

		try {
			Dept dep = this.getDeptService().get(dept.getId());
			BeanUtils.copyProperties(dept, dep);
			this.getDeptService().update(dep);
			this.msg = "修改成功";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String findAllDept() {

		try {

			String deptNo = this.getSessionAttr("user", User.class).getDeptNo()
					.trim();
			if (pagination == null) {
				pagination = new Pagination();
				pagination.setOrderBy("deptNo");

			    pagination.setOrderType("asc");
			    pagination.setPagesize(20);
			}

			pagination = this.getDeptService().findDeptByLike(pagination, dept,
					deptNo);
			url = "/swss" + this.getRequest().getServletPath();
			pagination.setUrl(url);
			// 设置字典
			List<?> fingSubList = this.getSysService().findSomeField("经费来源");
			List<?> deptNatureList = this.getSysService().findSomeField("单位性质");
			List<?> useArrowList = this.getSysService().findSomeField("使用方向");
			this.getRequest().setAttribute("fingSubList", fingSubList);
			this.getRequest().setAttribute("deptNatureList", deptNatureList);
			this.getRequest().setAttribute("useArrowList", useArrowList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}

	public String saveInit() {

		try {
			// 查询字典
			List<?> fingSubList = this.getSysService().findSomeField("经费来源");
			List<?> deptNatureList = this.getSysService().findSomeField("单位性质");
			List<?> useArrowList = this.getSysService().findSomeField("使用方向");
			this.getRequest().setAttribute("fingSubList", fingSubList);
			this.getRequest().setAttribute("deptNatureList", deptNatureList);
			this.getRequest().setAttribute("useArrowList", useArrowList);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "saveInit";
	}

	public String updateInit() {

		try {
			// 查询字典
			List<?> fingSubList = this.getSysService().findSomeField("经费来源");
			List<?> deptNatureList = this.getSysService().findSomeField("单位性质");
			List<?> useArrowList = this.getSysService().findSomeField("使用方向");
			this.getRequest().setAttribute("fingSubList", fingSubList);
			this.getRequest().setAttribute("deptNatureList", deptNatureList);
			this.getRequest().setAttribute("useArrowList", useArrowList);

			this.dept = this.getDeptService().get(dept.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "updateInit";
	}

	/**
	 * 单位条件筛选action
	 * 
	 * @return search 查询初始页面
	 */
	public String SearchInit() {
		try {
			 // 查询字典
			List<?> fingSubList = this.getSysService().findSomeField("经费来源");
			List<?> deptNatureList = this.getSysService().findSomeField("单位性质");
			List<?> useArrowList = this.getSysService().findSomeField("使用方向");
			this.getRequest().setAttribute("fingSubList", fingSubList);
			this.getRequest().setAttribute("deptNatureList", deptNatureList);
			this.getRequest().setAttribute("useArrowList", useArrowList);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "searchInit";
	}

	public String find() {

		try {

			String deptNo = this.getSessionAttr("user", User.class).getDeptNo()
					.trim();

			pagination = this.getDeptService().findDeptByLike(pagination, dept,
					deptNo);
			pagination.setUrl("/swss" + this.getRequest().getServletPath());
			// 设置字典
			List<?> fingSubList = this.getSysService().findSomeField("经费来源");
			List<?> deptNatureList = this.getSysService().findSomeField("单位性质");
			List<?> useArrowList = this.getSysService().findSomeField("使用方向");
			this.getRequest().setAttribute("fingSubList", fingSubList);
			this.getRequest().setAttribute("deptNatureList", deptNatureList);
			this.getRequest().setAttribute("useArrowList", useArrowList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		return "res";
	}

	public String tree() {
		
		try {
			this.print(this.deptService.findDeptTreeJSONByNo(deptNo));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}

		
		return null;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Pagination<?> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<?> pagination) {
		this.pagination = pagination;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public List<Dept> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Dept> deptList) {
		this.deptList = deptList;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public SysService getSysService() {
		return sysService;
	}

	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}

	public String getDeptNo() {
		return deptNo;
	}

	public void setDeptNo(String deptNo) {
		this.deptNo = deptNo;
	}

}
